<?php include $this->admin_tpl('meta', 'admin');?>
<link rel="stylesheet" href="<?php echo STATIC_URL;?>plugin/uploadify/3.2.1/uploadify.css"/>
<title>附件上传 - YzmCMS内容管理系统</title>
</head>
<body>
<div class="page-container">
	<div id="tab_system" class="HuiTab">
		<div class="tabBar cl"><span>上传文件</span><span>站内文件</span></div>
		<div class="tabCon">
			<span class="yzmcms_notice">类型:  <?php echo $type;?></span>
			<span class="yzmcms_notice">单文件最大: <span class="c-red"><?php echo $s;?></span> ，数量: <span class="c-red"><?php echo $n;?></span>个</span>
			<input id="file_upload" name="file_upload" type="file" multiple="true">
			<div id="uploadlist" class="imagelist">
				<ul>
				</ul>
			</div>
		</div>

		<div class="tabCon">
			<div class="imagelist">
				<ul>
					<?php
					foreach($data as $val){
						$thum = $val['isimage'] ? $val['filepath'].$val['filename'] : (in_array($val['fileext'], array('zip', 'rar')) ? STATIC_URL.'images/ext/rar.png' : STATIC_URL.'images/ext/blank.png');
						echo '<li><a href="javascript:;" onclick="javascript:album_cancel(this)" title="'.$val['originname'].'"><img class="img_src" src="'.$thum.'" path="'.$val['filepath'].$val['filename'].'"><img src="'.STATIC_URL.'images/checked.gif" class="checkd hidden"></a></li>';
					}
					?>
				</ul>
				<div class="clearfix"></div>
				 <div id="page"><span class="page_left">共<?php echo $total?>条记录</span><span class="page_right"><?php echo $page->gethome().$page->getlist().$page->getend();?></span></div>
			</div>
		</div>
	</div>
	<div class="bottom_button">
		<input type="button" class="btn btn-primary radius" id="determine" value="&nbsp;&nbsp;确定&nbsp;&nbsp;">
		<input type="button" class="btn btn-default radius" id="cancel" value="&nbsp;&nbsp;取消&nbsp;&nbsp;">
	</div>
</div>
<div id="att_status" class="hidden"></div>
<?php include $this->admin_tpl('footer', 'admin');?>
<script type="text/javascript" src="<?php echo STATIC_URL;?>plugin/uploadify/3.2.1/jquery.uploadify.min.js"></script>
<script type="text/javascript">
$(function(){
	$('#file_upload').uploadify({
		'width': 65,
		'height': 25,
		'uploadLimit' : <?php echo $n;?>, 
		'buttonText': '选择文件',
		'fileSizeLimit': '<?php echo $s;?>',
		'fileTypeExts' : '<?php echo $type;?>',
		'swf'      : '<?php echo STATIC_URL;?>plugin/uploadify/3.2.1/uploadify.swf',
		'uploader' : '<?php echo U('upload');?>',
		'successTimeout': 10,  
		'removeTimeout' : 0.2, 
		'onFallback':function(){
			layer.alert('您未安装FLASH控件，无法上传图片！请安装FLASH控件后再试。');
		},
		'formData' : { 
			'<?php echo session_name();?>' : '<?php echo session_id();?>',
            'type' : '<?php echo $t;?>',
            'module' : '<?php echo $module;?>',
        },
		'onUploadSuccess' : function(file, data, response) {
			data = $.parseJSON(data);
			
			if(data.status == 1){
				var att_url = data.filetype == 'jpg' || data.filetype == 'png' || data.filetype == 'gif' || data.filetype == 'jpeg' ? data.msg : (data.filetype == 'zip' || data.filetype == 'rar' ? '<?php echo STATIC_URL;?>images/ext/rar.png' : '<?php echo STATIC_URL;?>images/ext/blank.png');
				var li='<li><a href="javascript:;" class="on" onclick="javascript:album_cancel(this)"><img src="'+att_url+'" class="img_src" path="'+data.msg+'" title="'+data.title+'"/><img src="<?php echo STATIC_URL;?>images/checked.gif" class="checkd"></a></li>';
				$("#uploadlist ul").prepend(li);
				$('#att_status').append('|'+data.msg);
			}else{
				layer.alert(data.msg); 
			}
		}
	});
	
	var index = parent.layer.getFrameIndex(window.name);
	$('#determine').on('click', function(){
		var att_status = $("#att_status").html();
		if(att_status == ''){
			layer.alert('你还没选择附件呢？'); 
			return false;
		}
		att_status = att_status.substr(1);		
		<?php
			if($t == 1) echo 'if(!isimg(att_status)){ layer.alert("选择的类型必须为图片类型!"); return false;}';
		?>
		<?php if($n == 1) { ?>
		
		parent.$("#<?php echo $pid;?>").val(att_status);
		parent.$("#<?php echo $pid;?>_src").attr("src", att_status);
		
		<?php }else{ ?>
		
		var string = '';
		var strs= new Array(); 
		strs = att_status.split("|");
		for (i=0;i<strs.length;i++ ){ 
			string += '<li>文件：<input type="text" name="<?php echo $pid;?>[url][]" value="'+strs[i]+'" id="<?php echo $pid;?>_'+i+'" onMouseOver="yzm_img_preview(\'<?php echo $pid;?>_'+i+'\', this.value)"  onmouseout="layer.closeAll();" class="input-text w_300"> 描述：<input type="text" name="<?php echo $pid;?>[alt][]" value="" class="input-text w_200"><a href="javascript:;" onclick="remove_li(this);">删除</a></li>';
		} 
		parent.$("#<?php echo $pid;?>").append(string);	
		
		<?php } ?>
		parent.layer.close(index);
	});	
	
	$('#cancel').on('click', function(){
		parent.layer.close(index);
	});
	
	$.Huitab("#tab_system .tabBar span","#tab_system .tabCon","current","click","<?php echo isset($_GET['tab']) ? intval($_GET['tab']) : '0'?>");
});

function album_cancel(obj){
	var path = $(obj).children(".img_src").attr("path");
	if($(obj).hasClass('on')){
		$(obj).removeClass("on");
		$(obj).children(".checkd").addClass("hidden");
		var imgstr = $("#att_status").html();
		var length = $("a[class='on']").children(".img_src").length;
		var strs = '';
		for(var i=0;i<length;i++){
			strs += '|'+$("a[class='on']").children(".img_src").eq(i).attr('path');
		}
		$('#att_status').html(strs);
		
	}else{
		var num = $('#att_status').html().split('|').length;
		var file_upload_limit = '<?php echo $n;?>';
		if(num > file_upload_limit) {layer.alert('不能选择超过'+file_upload_limit+'个附件'); return false;}
		$(obj).addClass("on");
		$(obj).children(".checkd").removeClass("hidden");
		$('#att_status').append('|'+path);
	}
}

function isimg(url){
	var sTemp;
	var b = false;
	var opt = "jpg|gif|png|jpeg";
	var s=opt.toUpperCase().split("|");
	for (var i=0;i<s.length ;i++ ){
	sTemp = url.substr(url.length-s[i].length-1);
	sTemp = sTemp.toUpperCase();
	s[i] = "."+s[i];
	if (s[i]==sTemp){
		b = true;
		break;
	}
	}
	return b;
}
</script>

</body>
</html>